package org.zwd.computePrice.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.zwd.computePrice.dao.CrabDao;
import org.zwd.computePrice.dao.DBUtils;
import org.zwd.computePrice.dao.OrderDao;
import org.zwd.computePrice.dao.UserDao;
import org.zwd.computePrice.domain.Crab;
import org.zwd.computePrice.domain.Order;
import org.zwd.computePrice.domain.Total;
import org.zwd.computePrice.domain.User;
import org.zwd.computePrice.util.DateUtils;
import org.zwd.computePrice.util.UUIDUtils;

public class OrderService {
	private OrderDao orderDao = new OrderDao();
	private UserDao userDao = new UserDao();
	private CrabDao crabDao = new CrabDao();
	private Connection conn;
	
	public String  createOrder(List<Crab> crabList,String name,int userid,int isOrder){
		String orderId = null;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			
			orderId = UUIDUtils.getOrderIdByUUId();
			
			int totalPay = 0;
			int realPay = 0;
			double totalNumber = 0;
			double totalMaleNum = 0;
			double totalFemaleNum = 0;
			for(Crab crab : crabList){
				totalPay += crab.getTotalPrice();
				realPay = totalPay;
				totalNumber += crab.getNumber();
				if(crab.getIsmale() == 1){
					totalMaleNum += crab.getNumber();
					
				}
				if (crab.getIsmale() == 0) {
					totalFemaleNum += crab.getNumber();
				}
			}
			Order order = new Order();
			order.setId(orderId);
			order.setClientName(name);
			order.setTotalPay(totalPay);
			order.setRealPay(realPay);
			order.setTotalFemaleNum(totalFemaleNum);
			order.setTotalMaleNum(totalMaleNum);
			order.setTotalNumber(totalNumber);
			User user = userDao.findUserByid(userid);
			order.setUser(user);
			order.setIsOrder(isOrder);
			
			orderDao.insertOrder(conn,order);
			
			for(Crab crab : crabList){
				orderDao.insertOrderItem(conn,orderId, crab.getId(), crab.getCrabPrice().getPrice(), crab.getNumber(), crab.getNote());
			}
			
			conn.commit();
		} catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return orderId;
	}
	
	public void deleteOrderByid(String orderid){
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			
			orderDao.deleteOrderItemByid(conn, orderid);
			orderDao.deleteOrderByid(conn, orderid);
			conn.commit();
		} catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public Order searchOrderByOrderId(String orderId,int isOrder){
		Order order = null;
		order = orderDao.findOrderByOrderId(orderId,isOrder);
		if(order != null){
			User user = userDao.findUserByid(order.getUser().getId());
			order.setUser(user);
		}
		if(order != null){
			List<Crab> crabList = orderDao.findCrabByOrderId(orderId);
			order.setCrabList(crabList);
		}
		return order;
	}
	
	public Total getTotal(List<Order> orderList){
		Total total = new Total(orderList);
		Map<Integer, Double> crabmap = total.getCrabmap();
		List<Crab> totalCrabList = new ArrayList<Crab>();
		for(int id : crabmap.keySet()){
			Crab crab = crabDao.findCrabById(id);
			crab.setNumber(crabmap.get(crab.getId()));
			totalCrabList.add(crab);
		}
		total.setTotalCrabList(totalCrabList);
		return total;
	}
	
	public List<Order> searchOrderToday(int isOrder){
		List<Order> orderList = new ArrayList<Order>();
		List<String> orderidList = orderDao.findOrderIdByTime(DateUtils.getToday(),DateUtils.getTomorrow(),isOrder);
		for(String orderid : orderidList){
			Order order = searchOrderByOrderId(orderid,isOrder);
			orderList.add(order);
		}
		return orderList;	
	}
	
	public List<Order> searchOrderByTime(String startTime, String endTime,int isOrder){
		List<Order> orderList = new ArrayList<Order>();
		List<String> orderidList = orderDao.findOrderIdByTime(startTime,endTime,isOrder);
		for(String orderid : orderidList){
			Order order = searchOrderByOrderId(orderid,isOrder);
			orderList.add(order);
		}
		return orderList;	
	}
	
	public List<Order> searchOrderByName(String name,int isOrder){
		List<Order> orderList = new ArrayList<Order>();
		List<String> orderidList = orderDao.findOrderIdByName(name,isOrder);
		for(String orderid : orderidList){
			Order order = searchOrderByOrderId(orderid,isOrder);
			orderList.add(order);
		}
		return orderList;	
	}
	
	public void updateRealPayByOrderid(String orderid,int realPay){
		orderDao.updateRealPayById(orderid, realPay);
	}
	
	public void updateOrder(Order order){
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			orderDao.updateOrder(conn, order);
			for(Crab crab:order.getCrabList())
				orderDao.updateOrderItem(conn, order.getId(), crab);
			conn.commit();
		} catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}
